package com.wutong.erb.detail;

import com.wutong.erb.entity.user.User;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;

/**
 * 用户详情
 * @author wutong
 */
public record UserAuthDetail(User user) implements UserDetails {

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.singletonList(new SimpleGrantedAuthority(
                "ROLE_" + user.getUserType().name().toUpperCase()
        ));
    }

    @Override
    public String getPassword() {
        return user.getPasswordHash();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        // 账户是否未过期（如需过期功能可关联user表的过期时间字段）
        return user.getStatus() != User.UserStatus.EXPIRED;
    }

    @Override
    public boolean isAccountNonLocked() {
        return user.getStatus() != User.UserStatus.LOCKED;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // 凭证（密码）是否未过期（如需密码定期更换功能）
        // 默认不启用密码过期
        return true;
    }

    @Override
    public boolean isEnabled() {
        // 账户是否启用（对应user表的status字段）
        return user.getStatus() == User.UserStatus.NORMAL;
    }
}
